﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Web.Profile;

namespace WebSite.cn
{
    public partial class register_modify : System.Web.UI.Page
    {
        // Fields
        public string tmpAddress = "";
        public string tmpCname = "";
        public string tmpEmail = "";

        public string tmpMsn = "";
        public string tmpOldpas = "";
        public string tmpPhone = "";
        public string tmpPost = "";
        public string tmpQQ = "";
        public string tmpSex = "";
        public string tmpTel = "";
        public string tmpUsername = "";

        // Methods
        private void Bind()
        {
            DataTable table = SQLAccess.GreatDs("select memberlogin,memberpwd,membername,sex,tel1,phone,email,msn,qq,address,post from DB_Member where memberid=" + this.Session["memberid"] + "").Tables[0];
            if (table.Rows.Count > 0)
            {
                this.tmpUsername = table.Rows[0]["memberlogin"].ToString();
                this.tmpCname = table.Rows[0]["membername"].ToString();
                this.tmpSex = table.Rows[0]["sex"].ToString();
                this.tmpPhone = table.Rows[0]["phone"].ToString();
                this.tmpTel = table.Rows[0]["tel1"].ToString();
                this.tmpPost = table.Rows[0]["post"].ToString();
                this.tmpAddress = table.Rows[0]["address"].ToString();
                this.tmpMsn = table.Rows[0]["msn"].ToString();
                this.tmpQQ = table.Rows[0]["qq"].ToString();
                this.tmpEmail = table.Rows[0]["email"].ToString();

                this.username.Value = tmpUsername;
                this.cname.Value = tmpCname;
                this.email.Value = tmpEmail;
                if (this.tmpSex == "0" || this.tmpSex == "1")
                {
                    this.rbtSex.Items.FindByValue(this.tmpSex).Selected = true;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string str3;
            DataTable table;
            string str = this.Session["username"].ToString();
            string str4 = "";
            if (this.username.Value != this.Session["username"].ToString())
            {
                table = SQLAccess.GreatDs(string.Concat(new object[] { "select memberid from DB_Member where memberlogin='", this.tmpUsername, "' and memberid<>", this.Session["memberid"], "" })).Tables[0];
                if (table.Rows.Count > 0)
                {
                    base.Response.Write("<script language=javascript>alert('用户名已存在');window.history.go(-1);</script>");
                    base.Response.End();
                }
                else
                {
                    str = base.Request.Form["uname"];
                }
            }
            if (string.IsNullOrEmpty(this.oldpas.Value))
            {
                table = SQLAccess.GreatDs(string.Concat(new object[] { "select memberid from DB_Member where memberpwd='", FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.oldpas.Value), "MD5"), "' and memberid=", this.Session["memberid"], "" })).Tables[0];
                if (table.Rows.Count > 0)
                {
                    str4 = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.newpas.Value), "MD5");
                }
                else
                {
                    base.Response.Write("<script language=javascript>alert('原始密码输入有误');window.history.go(-1);</script>");
                    base.Response.End();
                }
            }
            if (str4 != "")
            {
                str3 = string.Concat(new object[] { 
                "update DB_Member set memberlogin='", str, "',memberpwd='", str4, "',membername='", this.cname.Value, "',sex='",this.rbtSex.SelectedItem.Value, "',tel1='", base.Request.Form["tel"], "',phone='", base.Request.Form["phone"], "',email='", this.email.Value, "',msn='", base.Request.Form["msn"], 
                "',qq='", base.Request.Form["qq"], "',address='", base.Request.Form["address"], "',post='", base.Request.Form["post"], "' where memberid=", this.Session["memberid"], ""
             });
            }
            else
            {
                str3 = string.Concat(new object[] { 
                "update DB_Member set memberlogin='", str, "',membername='", this.cname.Value, "',sex='","", "',tel1='", base.Request.Form["tel"], "',phone='", base.Request.Form["phone"], "',email='", this.email.Value, "',msn='", base.Request.Form["msn"], "',qq='", base.Request.Form["qq"], 
                "',address='", base.Request.Form["address"], "',post='", base.Request.Form["post"], "' where memberid=", this.Session["memberid"], ""
             });
            }
            SQLAccess.DoSql(str3);
            base.Response.Write("<script language=javascript>alert('更新成功');window.location.href='register_modify.aspx';</script>");
            base.Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                if (this.Session["memberid"] != null)
                {
                    this.Bind();
                }
                else
                {
                    base.Response.Write("<script language=javascript>window.location.href='index.aspx';</script>");
                    base.Response.End();
                }
            }
        }


    }
}